<template>
  <div class="app-container">
    <!-- 操作按钮 - 右上角 -->
    <div style="text-align: right; margin-bottom: 20px;">
      <el-button
        type="primary"
        icon="Plus"
        @click="handleAdd"
        v-hasPermi="['system:purchase:add']"
      >发布信息</el-button>
    </div>

    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发布者" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入发布者"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
             <el-form-item label="状态" prop="status">
                   <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 200px;">
            <el-option label="未上架" :value="0" />
            <el-option label="已上架" :value="1" />
            <el-option label="已通过审核" :value="2" />
            <el-option label="审核不通过" :value="3" />
            <el-option label="活动已下架" :value="5" />
          </el-select>
       </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single || !canEdit"
          @click="handleUpdate"
          v-hasPermi="['system:purchase:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:purchase:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:purchase:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="purchaseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="发布者" align="center" prop="publisher" />
      <el-table-column label="发布时间" align="center" prop="time" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布内容" align="center" prop="content" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ getStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleDetail(scope.row)">详情</el-button>
          <el-button 
            v-if="canEditItem(scope.row.status)" 
            link 
            type="primary" 
            icon="Edit" 
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button 
            v-if="canShelfUp(scope.row.status)" 
            link 
            type="success" 
            icon="Top" 
            @click="handleShelfUp(scope.row)"
          >上架</el-button>
          <el-button 
            v-if="canShelfDown(scope.row.status)" 
            link 
            type="warning" 
            icon="Bottom" 
            @click="handleShelfDown(scope.row)"
          >下架</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />




  </div>
</template>

<script setup name="Purchase">
import { listPurchase, delPurchase, shelfUpPurchase, shelfDownPurchase } from "@/api/system/purchase"

const { proxy } = getCurrentInstance()

const purchaseList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const canEdit = ref(false)

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    publisher: null,
    content: null,
    status: null,
  }
})

const { queryParams } = toRefs(data)

/** 查询优惠团购列表 */
function getList() {
  loading.value = true
  listPurchase(queryParams.value).then(response => {
    purchaseList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}



/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  queryParams.value.status = null
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
  
  // 检查是否可以编辑（只有下架状态才能编辑）
  if (selection.length === 1) {
    canEdit.value = selection[0].status === 0
  } else {
    canEdit.value = false
  }
}

/** 新增按钮操作 */
function handleAdd() {
  // 跳转到发布页面
  proxy.$router.push('/system/purchase/publish')
}



/** 修改按钮操作 */
function handleUpdate(row) {
  // 跳转到编辑页面
  const _id = row.id || ids.value
  proxy.$router.push(`/system/purchase/edit/${_id}`)
}

/** 详情按钮操作 */
function handleDetail(row) {
  // 跳转到详情页面
  proxy.$router.push(`/system/purchase/detail/${row.id}`)
}

/** 上架按钮操作 */
function handleShelfUp(row) {
  proxy.$modal.confirm('是否确认上架标题为"' + row.title + '"的数据项？').then(function() {
    return shelfUpPurchase(row.id)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("上架成功")
  }).catch(() => {})
}

/** 下架按钮操作 */
function handleShelfDown(row) {
  proxy.$modal.confirm('是否确认下架标题为"' + row.title + '"的数据项？').then(function() {
    return shelfDownPurchase(row.id)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("下架成功")
  }).catch(() => {})
}



/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除优惠团购编号为"' + _ids + '"的数据项？').then(function() {
    return delPurchase(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/purchase/export', {
    ...queryParams.value
  }, `purchase_${new Date().getTime()}.xlsx`)
}

/** 获取状态类型 */
function getStatusType(status) {
  switch (status) {
    case 0: return 'info'    // 未上架
    case 1: return 'success' // 已上架
    case 2: return 'success' // 已通过审核
    case 3: return 'danger'  // 审核不通过
    case 5: return 'warning' // 活动已下架
    default: return 'info'
  }
}

/** 获取状态文本 */
function getStatusText(status) {
  switch (status) {
    case 0: return '未上架'
    case 1: return '已上架'
    case 2: return '已通过审核'
    case 3: return '审核不通过'
    case 5: return '活动已下架'
    default: return '未知'
  }
}

/** 判断是否可以编辑 */
function canEditItem(status) {
  return status === 0 // 只有未上架状态可以编辑
}

/** 判断是否可以上架 */
function canShelfUp(status) {
  return status === 2 // 只有已通过审核状态可以上架，活动已下架状态不能上架
}

/** 判断是否可以下架 */
function canShelfDown(status) {
  return status === 1 // 只有已上架状态可以下架
}

getList()
</script>
